package com.cantrill.ssofilter;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
 * HttpRequest wrapper passed down the chain by SSOFilter
 * @author subin
 *
 */
public class SSORequest extends HttpServletRequestWrapper
	{
		private String username=null;
		private String sessionid;
		private List<String> roles=new ArrayList<String>();
		public SSORequest(HttpServletRequest request) 
		{
			
			super(request);
			
		}
		public SSORequest(HttpServletRequest request,String username,String rolesstring,String sessionid) 
		{
			
			this(request);
			this.username=username;
			if(!"".equals(rolesstring)&& !(rolesstring==null))
			{
				String[] rs=rolesstring.split("::");
				for(String role:rs)
				{
					roles.add(role);
				}
			}
		setSessionid(sessionid);
		}
		public void setUsername(String username) {
			this.username = username;
		}
		/**
		 * get username of currently logged in user
		 * @return
		 */
		public String getUsername() {
			return username;
		}
		@Override
		public boolean isUserInRole(String rolename)
		{
			for(String role:roles)
			{
				if(role.equals(rolename))
				{
					return true;
				}
			}
			return false;
		}
		/**
		 * get roles of currently logged in user separated by ::
		 * @return
		 */
		public String getRolesAsString()
		{
			String str="";
			for(String s:roles)
			{
				str=str+"::"+s;
			}
			return str;
		}
		public void setSessionid(String sessionid) {
			this.sessionid = sessionid;
		}
		/**
		 * get session id of current session
		 * @return
		 */
		public String getSessionid() {
			return sessionid;
		}

}
